package com.zhangtao.demo;

import com.zhangtao.demo.common.util.GlobalComponentHolder;
import com.zhangtao.demo.config.netty.websocket.NioWebSocketServer;
import com.zhangtao.demo.config.redis.jedis.JedisApi;
import com.zhangtao.demo.controller.JedisController;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.zhangtao.demo.common.util.BaiduAipImageClassifyClient;
import com.zhangtao.demo.common.util.BaiduSpeechApi;
import com.zhangtao.demo.config.redis.jedis.RedisPool;

/**
 * @author zhnagtao
 */
@SpringBootApplication
public class CommonUtilsApplication {
	@Autowired
	private JedisApi redisApi;
	@Value(value = "${baiduaip.appid}")
	private String baiduAppId;
	@Value(value = "${baiduaip.appkey}")
	private String baiduAppKey;
	@Value(value = "${baiduaip.secretkey}")
	private String baiduSecretKey;
	private static final Logger logger = LogManager.getLogger(CommonUtilsApplication.class);

	public static void main(String[] args) {
//		// 可选：设置log4j日志输出格式，若不设置，则使用默认配置
//        // 也可以直接通过jvm启动参数设置此环境变量
		System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
		SpringApplication.run(CommonUtilsApplication.class, args);
		logger.info("redisclient url : http://localhost:8777/jedis-gui");
		logger.info("index url : http://localhost:8777");
		String exePath = null;
		if (args != null && args.length > 0) {
			exePath = args[0];
		}
		String cmd = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe http://localhost:8777";
		if (StringUtils.isNotBlank(exePath)) {
			cmd = exePath + " http://localhost:8777";
		}
		Runtime run = Runtime.getRuntime();
		try {
			run.exec(cmd);
			logger.debug("启动浏览器打开项目成功");
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	@Autowired
	JedisController jedisController;
	@Bean
	public Integer initComponent() {
		GlobalComponentHolder.redisApi = redisApi;
		GlobalComponentHolder.jedisController = jedisController;
		return 1;
	}
	@Bean
	public RedisPool initJedisPool() {
		RedisPool rp = new RedisPool("localhost", 6379, null, 0);
		return rp;
	}

	@Bean
	public BaiduSpeechApi initBaiduSpeech() {
		return new BaiduSpeechApi(baiduAppId, baiduAppKey, baiduSecretKey);
	}

	@Bean
	public BaiduAipImageClassifyClient initBaiduImage() {
		return new BaiduAipImageClassifyClient(baiduAppId, baiduAppKey, baiduSecretKey);
	}
}
